package proj2501.simple_module;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

class P006XML {
	public P006XML() {
		System.out.println(345);
		write1();
	}
	
	private void write1() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new RuntimeException();
		}
		DOMImplementation implementation = builder.getDOMImplementation();
		Document document = implementation.createDocument("", "root", null);
		
		Element rootElement = document.getDocumentElement();
		Element element1 = document.createElement("element1");
		element1.appendChild(document.createTextNode("text1"));
		rootElement.appendChild(element1);
		Element element2 = document.createElement("element2");
		element2.appendChild(document.createTextNode("text2"));
		rootElement.appendChild(element2);
		
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer;
		try {
			transformer = tFactory.newTransformer();
		} catch (TransformerConfigurationException e) {
			throw new RuntimeException();
		}
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(new File("test1.xml"));
		try {
			transformer.transform(source, result);
		} catch (TransformerException e) {
			throw new RuntimeException();
		}
	}
}
